#include <iostream>
#include <iomanip>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

extern "C" {
#include "sis3100_vme_calls.h"
#include "timemeasure.h"
}

#include "DigitalIO_DL636F.h"

using namespace std;

int crate;
char *dev_fn     = "/tmp/sis1100_00remote";
u_int32_t io_addr      = 0x8800;

int main(int argc,char *argv[])
{
		cerr << "test_DigitalIO   built:" << __DATE__ << " " << __TIME__
				 << endl << endl;

		// open sis1100/3100 pci-to-vme link
		if( !(crate=open(dev_fn, O_RDWR, 0)) )
		{
				cerr << "ERROR: could not open \"" << dev_fn << "\"" << endl;
				cerr.flush(); exit(1);
		}

		DigitalIO_DL636F io(crate,io_addr);

		cerr << setw(20) << "vmesysreset..." << flush;
		if(int r=vmesysreset(crate)) {
				cerr << "error: vmesysreset() returned " << r << endl << endl;
				cerr.flush(); exit(1);
		}
		cerr << "ok   sleeping 1 sec\n"; sleep(1);

		try {
				for(int i=0; i<100000; i++) {

						io.setOutputs(0);
						
						cerr << hex
								 << "\nStatus of DL6363F:\n\n"
								 << "outputs=0x" << io.getOutputs() << "\n"
								 << "input=0x"   << io.getInput() << "\n"
								 << "output1="   << io.getO1() << "\n"
								 << "output2="   << io.getO2() << "\n"
								 << "output3="   << io.getO3() << "\n"
								 << "output4="   << io.getO4() << "\n"
								 << dec << endl;

						io.setOutputs(0xf);

						cerr << hex
								 << "\nStatus of DL6363F:\n\n"
								 << "outputs=0x" << io.getOutputs() << "\n"
								 << "input=0x"   << io.getInput() << "\n"
								 << "output1="   << io.getO1() << "\n"
								 << "output2="   << io.getO2() << "\n"
								 << "output3="   << io.getO3() << "\n"
								 << "output4="   << io.getO4() << "\n"
								 << dec << endl;
				}
		} catch(IOError ioe) {
				cerr << "catched IOError:\n"
						 << ioe.getMsg() << "\n"
						 << "param=" << ioe.getParam() << "\n\n";
		}

		io.setOutputs(0x0);

		close(crate);
}
